package com.ostrich.library_common.views

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView





/**
 * @Author : yaotianxue
 * @Time : On 2023/10/31 09:24
 * @Description : CustomItemDe
 */
class CustomItemDecoration(val count:Int,val space:Int):RecyclerView.ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State,
    ) {
//        super.getItemOffsets(outRect, view, parent, state)
       //第一行 上 0  最后一行 下 0 左边一列 左 0 右边一列 右 0
        //1:获得当前item的下标
        outRect.left = space/2
        outRect.right = space/2
        outRect.top = space/2
        outRect.bottom = space/2

        val position = parent.getChildAdapterPosition(view)//获得当前item下标
        //左
        if(position % count == 0){
            outRect.left = space
        }
        //右
        if(position % count == count - 1){
            outRect.right = space
        }
        //第一行
        if(position < count){
            outRect.top = space
        }
        //最后一行
        parent.adapter?.let {
            val size = it.itemCount / count //获得行数
            if(it.itemCount % count == 0){//整整齐齐排满
                if(position >= (size -1 )* count){
                    outRect.bottom = space
                }
            }else{//最后一行有空位
                if(position >= size * count){
                    outRect.bottom = space
                }
            }
        }



    }
}